/************************************************************************
 * @file ServiceRegistry.cpp
 * @author Ian Chen
 * @version 1.0
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 *
 ************************************************************************/
#include <plugin/service/ServiceRegistry.h>
#include <plugin/service/AbstractService.h>

namespace antu
{
namespace plugin
{
ServiceRegistry::ServiceRegistry(QObject *parent) :
    QObject(parent)
{
}

ServiceRegistry::~ServiceRegistry()
{
    this->m_oServices.clear();
}

void ServiceRegistry::registerService(const QString &pluginCode, AbstractService *pService)
{
    Q_ASSERT(!pluginCode.isNull() && !pluginCode.isEmpty());
    Q_ASSERT(pService != nullptr);

    ServiceMap::iterator iter = this->m_oServices.find(pluginCode);

    if (iter == this->m_oServices.end())
    {
        ServiceList services;
        this->m_oServices.insert(pair<QString, ServiceList>(pluginCode, services));
        iter = this->m_oServices.find(pluginCode);
    }
    iter->second.push_back(pService);
    emit serviceAvailable(pluginCode, pService->serviceName(), pService);
}

}
}

